home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / exampleCode / opengl / xlib / ttri.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  13.5 KB  |  622 lines

  1. /*
  2.  * Copyright 1993, 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. #include <stdio.h>
  18. #include <string.h>
  19. #include <stdlib.h>
  20. #include <GL/gl.h>
  21. #include <GL/glx.h>
  22. #include <GL/glu.h>
  23. #include <X11/keysym.h>
  24.  
  25. static int attributes[] = {
  26.     GLX_DOUBLEBUFFER,
  27.     GLX_RGBA,
  28.     GLX_RED_SIZE, 1,
  29.     GLX_GREEN_SIZE, 1,
  30.     GLX_BLUE_SIZE, 1,
  31.     None,
  32. };
  33.  
  34. #define SETCOLOR(x) (rgb ? glColor3fv(rgbMap[x]) : glIndexf(x))
  35.  
  36. enum {
  37.     BLACK = 0,
  38.     RED,
  39.     GREEN,
  40.     YELLOW,
  41.     BLUE,
  42.     MAGENTA,
  43.     CYAN,
  44.     WHITE
  45. };
  46.  
  47. #define    SOLID 1
  48. #define    LINE 2
  49. #define    POINT 3
  50.  
  51.  
  52. static float rgbMap[8][3] = {
  53.     {0, 0, 0},
  54.     {1, 0, 0},
  55.     {0, 1, 0},
  56.     {1, 1, 0},
  57.     {0, 0, 1},
  58.     {1, 0, 1},
  59.     {0, 1, 1},
  60.     {1, 1, 1}
  61. };
  62.  
  63. static long width = 300, height = 300;
  64. static long needUpdate;
  65. static long rgb = 1;
  66. static long color1, color2, color3;
  67.  
  68. static float zRotation = 90.0;
  69. static float zoom = 1;
  70. static long state = SOLID;
  71. static long showVerticies = 1;
  72. static long hideBottomTriangle = GL_FALSE;
  73. static long dithering = GL_TRUE;
  74. static long outline = GL_TRUE;
  75. static long culling = GL_FALSE;
  76. static long winding = GL_FALSE;
  77. static long face = GL_FALSE;
  78.  
  79. static float boxA[3] = {-100, -100, 0};
  80. static float boxB[3] = { 100, -100, 0};
  81. static float boxC[3] = { 100,  100, 0};
  82. static float boxD[3] = {-100,  100, 0};
  83.  
  84. static float p0[3] = {-125,-80, 0};
  85. static float p1[3] = {-125, 80, 0};
  86. static float p2[3] = { 172,  0, 0};
  87.  
  88.  
  89. static void BeginPrim(void)
  90. {
  91.     switch (state) {
  92.       case SOLID:
  93.     glBegin(GL_POLYGON);
  94.     break;
  95.       case LINE:
  96.     glBegin(GL_LINE_LOOP);
  97.     break;
  98.       case POINT:
  99.     glBegin(GL_POINTS);
  100.     break;
  101.     }
  102. }
  103.  
  104. static void EndPrim(void)
  105. {
  106.     glEnd();
  107. }
  108.  
  109. static void DoDisplay(void)
  110. {
  111.     float xscale, yscale;
  112.  
  113.     (culling) ? glEnable(GL_CULL_FACE) : glDisable(GL_CULL_FACE);
  114.     (winding) ? glFrontFace(GL_CCW) : glFrontFace(GL_CW);
  115.     (face) ? glCullFace(GL_FRONT) : glCullFace(GL_BACK);
  116.  
  117.     glScissor(10, 10, width-20, height-20);
  118.     glEnable(GL_SCISSOR_TEST);
  119.     glViewport(10, 10, width-20, height-20);
  120.     glMatrixMode(GL_PROJECTION);
  121.     glLoadIdentity();
  122.     gluOrtho2D(-175, 175, -175, 175);
  123.     glMatrixMode(GL_MODELVIEW);
  124.     (dithering) ? glEnable(GL_DITHER) : glDisable(GL_DITHER);
  125.  
  126.     glClearColor(0.0, 0.0, 0.0, 0.0);
  127.     glClear(GL_COLOR_BUFFER_BIT);
  128.  
  129.     /* draw clipping box */
  130.     SETCOLOR(GREEN);
  131.     glBegin(GL_LINE_LOOP);
  132.     glVertex3fv(boxA);
  133.     glVertex3fv(boxB);
  134.     glVertex3fv(boxC);
  135.     glVertex3fv(boxD);
  136.     glEnd();
  137.  
  138.     /* draw triangle to show where it sits in relation to clipping box */
  139.     if (!hideBottomTriangle) {
  140.     SETCOLOR(BLUE);
  141.     glPushMatrix();
  142.     glScalef(zoom, zoom, zoom);
  143.     glRotatef(zRotation, 0,0,1);
  144.     BeginPrim();
  145.         glVertex3fv(p0);
  146.         glVertex3fv(p1);
  147.         glVertex3fv(p2);
  148.     EndPrim();
  149.     if (showVerticies) {
  150.         rgb ? glColor3fv(rgbMap[RED]) : glIndexf(color1);
  151.         glRectf(p0[0]-2, p0[1]-2, p0[0]+2, p0[1]+2);
  152.         rgb ? glColor3fv(rgbMap[GREEN]) : glIndexf(color2);
  153.         glRectf(p1[0]-2, p1[1]-2, p1[0]+2, p1[1]+2);
  154.         rgb ? glColor3fv(rgbMap[BLUE]) : glIndexf(color3);
  155.         glRectf(p2[0]-2, p2[1]-2, p2[0]+2, p2[1]+2);
  156.     }
  157.     glPopMatrix();
  158.     }
  159.  
  160.     /* now draw triangle with frustum clipping to clipping box */
  161.     SETCOLOR(RED);
  162.     xscale = (float)(width - 20) / 2 / 175 * (175 - 100) + 10;
  163.     yscale = (float)(height - 20) / 2 / 175 * (175 - 100) + 10;
  164.     glMatrixMode(GL_PROJECTION);
  165.     glLoadIdentity();
  166.     gluOrtho2D(-100, 100, -100, 100);
  167.     glMatrixMode(GL_MODELVIEW);
  168.     glScissor(xscale, yscale, width-2*xscale, height-2*yscale);
  169.     glViewport(xscale, yscale, width-2*xscale, height-2*yscale);
  170.     glPushMatrix();
  171.     glScalef(zoom, zoom, zoom);
  172.     glRotatef(zRotation, 0,0,1);
  173.  
  174.     glEnable(GL_LINE_STIPPLE);
  175.     glLineWidth(5);
  176.     glEnable(GL_POINT_SMOOTH);
  177.     glPointSize(10);
  178.     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  179.     BeginPrim();
  180.             rgb ? glColor3fv(rgbMap[RED]) : glIndexf(color1);
  181.         glVertex3fv(p0);
  182.         rgb ? glColor3fv(rgbMap[GREEN]) : glIndexf(color2);
  183.         glVertex3fv(p1);
  184.         rgb ? glColor3fv(rgbMap[BLUE]) : glIndexf(color3);
  185.         glVertex3fv(p2);
  186.     EndPrim();
  187.     glDisable(GL_LINE_STIPPLE);
  188.     glLineWidth(1);
  189.     glDisable(GL_POINT_SMOOTH);
  190.     glPointSize(1);
  191.     glBlendFunc(GL_ONE, GL_ZERO);
  192.  
  193.         if (outline) {
  194.         SETCOLOR(WHITE);
  195.         glBegin(GL_LINE_LOOP);
  196.             glVertex3fv(p0);
  197.             glVertex3fv(p1);
  198.             glVertex3fv(p2);
  199.         glEnd();
  200.         }
  201.     glPopMatrix();
  202.  
  203.     glFlush();
  204.  
  205.     if (needUpdate) {
  206.     printf("%s %s %s\n",
  207.            (culling ? "Culling" : "Not culling"),
  208.            (face ? "front" : "back"),
  209.            (winding ? "ccw" : "cw"));
  210.     needUpdate = 0;
  211.     }
  212. }
  213.  
  214. #if XXX
  215. static void SetColorMap(void)
  216. {
  217.     TK_ColorRec c;
  218.     float percent, percent2;
  219.     long j, i;
  220.     float r, g, b;
  221.  
  222.     for (j = 0; j <= 12; j++) {
  223.     if (j <= 6) {
  224.         percent = j / 6.0;
  225.         r = 1.0 - 0.8 * percent;
  226.         g = 0.2 + 0.8 * percent;
  227.         b = 0.2;
  228.     } else {
  229.         percent = (j - 6) / 6.0;
  230.         r = 0.2;
  231.         g = 1.0 - 0.8 * percent;
  232.         b = 0.2 + 0.8 * percent;
  233.     }
  234.     c.index = j + 18;
  235.     c.r = r;
  236.     c.g = g;
  237.     c.b = b;
  238.     tkSet(TK_CITORGB, (void *)&c);
  239.     for (i = 0; i < 16; i++) {
  240.         percent2 = i / 15.0;
  241.         c.index = j * 16 + i + 32;
  242.         c.r = r * percent2;
  243.         c.g = g * percent2;
  244.         c.b = b * percent2;
  245.         tkSet(TK_CITORGB, (void *)&c);
  246.     }
  247.     }
  248.     color1 = 18;
  249.     color2 = 24;
  250.     color3 = 30;
  251. }
  252. #endif
  253.  
  254. #if XXX
  255. static long Exec(TK_EventRec *ptr)
  256. {
  257.     switch (ptr->event) {
  258.       case TK_EVENT_EXPOSE:
  259.     tkPostEvent(&drawEvent);
  260.     break;
  261.       case TK_EVENT_CONFIG:
  262.     W = ptr->data[TK_WINDOWX];
  263.     H = ptr->data[TK_WINDOWY];
  264.     tkPostEvent(&drawEvent);
  265.     break;
  266.       case TK_EVENT_KEY:
  267.     switch (ptr->data[TK_KEY]) {
  268.       case TK_ESCAPE:
  269.         tkQuit();
  270.       case TK_LEFT:
  271.         zRotation += 0.5;
  272.         tkPostEvent(&drawEvent);
  273.         break;
  274.       case TK_RIGHT:
  275.         zRotation -= 0.5;
  276.         tkPostEvent(&drawEvent);
  277.         break;
  278.       case TK_UP:
  279.         zoom *= 0.75;
  280.         tkPostEvent(&drawEvent);
  281.         break;
  282.       case TK_DOWN:
  283.         zoom /= 0.75;
  284.         if (zoom > 10) {
  285.         zoom = 10;
  286.         }
  287.         tkPostEvent(&drawEvent);
  288.         break;
  289.       case TK_f:
  290.       case TK_F:
  291.         glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  292.         tkPostEvent(&drawEvent);
  293.         break;
  294.       case TK_p:
  295.       case TK_P:
  296.         glPolygonMode(GL_FRONT_AND_BACK, GL_POINT);
  297.         tkPostEvent(&drawEvent);
  298.         break;
  299.       case TK_l:
  300.       case TK_L:
  301.         glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  302.         tkPostEvent(&drawEvent);
  303.         break;
  304.       case TK_1:
  305.         state = SOLID;
  306.         tkPostEvent(&drawEvent);
  307.         break;
  308.       case TK_2:
  309.         state = LINE;
  310.         tkPostEvent(&drawEvent);
  311.         break;
  312.       case TK_3:
  313.         state = POINT;
  314.         tkPostEvent(&drawEvent);
  315.         break;
  316.       case TK_A:
  317.       case TK_a:
  318.         zRotation = 0.0;
  319.         tkPostEvent(&drawEvent);
  320.         break;
  321.       case TK_B:
  322.       case TK_b:
  323.         zRotation = 90.0;
  324.         tkPostEvent(&drawEvent);
  325.         break;
  326.       case TK_C:
  327.       case TK_c:
  328.         zRotation = 180.0;
  329.         tkPostEvent(&drawEvent);
  330.         break;
  331.       case TK_D:
  332.       case TK_d:
  333.         zRotation = 270.0;
  334.         tkPostEvent(&drawEvent);
  335.         break;
  336.       case TK_V:
  337.       case TK_v:
  338.         showVerticies = !showVerticies;
  339.         tkPostEvent(&drawEvent);
  340.         break;
  341.       case TK_s:
  342.       case TK_S:
  343.         glShadeModel(GL_SMOOTH);
  344.         tkPostEvent(&drawEvent);
  345.         break;
  346.       case TK_t:
  347.       case TK_T:
  348.         glShadeModel(GL_FLAT);
  349.         tkPostEvent(&drawEvent);
  350.         break;
  351.       case TK_h:
  352.       case TK_H:
  353.         hideBottomTriangle = !hideBottomTriangle;
  354.         tkPostEvent(&drawEvent);
  355.         break;
  356.       case TK_o:
  357.       case TK_O:
  358.         outline = !outline;
  359.         tkPostEvent(&drawEvent);
  360.         break;
  361.       case TK_m:
  362.       case TK_M:
  363.         dithering = !dithering;
  364.         tkPostEvent(&drawEvent);
  365.         break;
  366.       case TK_7:
  367.         culling = !culling;
  368.         tkPostEvent(&drawEvent);
  369.         needUpdate=1;
  370.         break;
  371.       case TK_8:
  372.         winding = !winding;
  373.         tkPostEvent(&drawEvent);
  374.         needUpdate=1;
  375.         break;
  376.       case TK_9:
  377.         face = !face;
  378.         tkPostEvent(&drawEvent);
  379.         needUpdate=1;
  380.         break;
  381.       case TK_Q:
  382.       case TK_q:
  383.         glEnable(GL_POLYGON_SMOOTH);
  384.         glEnable(GL_BLEND);
  385.         glBlendFunc(GL_SRC_ALPHA, GL_ONE);
  386.         if (!rgb) {
  387.         color1 = 32;
  388.         color2 = 128;
  389.         color3 = 224;
  390.         }
  391.         tkPostEvent(&drawEvent);
  392.         break;
  393.       case TK_W:
  394.       case TK_w:
  395.         glDisable(GL_POLYGON_SMOOTH);
  396.         glDisable(GL_BLEND);
  397.         if (!rgb) {
  398.         color1 = 18;
  399.         color2 = 24;
  400.         color3 = 30;
  401.         }
  402.         tkPostEvent(&drawEvent);
  403.         break;
  404.     }
  405.     break;
  406.       case TK_EVENT_DRAW:
  407.     DoDisplay();
  408.     break;
  409.     }
  410.     return 1;
  411. }
  412. #endif
  413.  
  414. static Bool WaitForMapNotify(Display *d, XEvent *e, char *arg)
  415. {
  416.     if ((e->type == MapNotify) && (e->xmap.window == (Window)arg)) {
  417.     return GL_TRUE;
  418.     }
  419.     return GL_FALSE;
  420. }
  421.  
  422. int main(int argc, char *argv[])
  423. {
  424.     XVisualInfo *vi;
  425.     Display *dpy;
  426.     Colormap cmap;
  427.     Window window;
  428.     XSetWindowAttributes swa;
  429.     GLXContext cx;
  430.     XEvent event;
  431.     GLboolean needDisplay;
  432.  
  433.     dpy = XOpenDisplay(0);
  434.     if (!dpy) {
  435.     fprintf(stderr, "Can't connect to display \"%s\"\n", getenv("DISPLAY"));
  436.     return -1;
  437.     }
  438.  
  439.     vi = glXChooseVisual(dpy, DefaultScreen(dpy), attributes);
  440.     if (!vi) {
  441.     fprintf(stderr, "No singlebuffered rgba visual on \"%s\"\n",
  442.         getenv("DISPLAY"));
  443.     return -1;
  444.     }
  445.  
  446.     cmap = XCreateColormap(dpy, RootWindow(dpy, vi->screen), vi->visual,
  447.                AllocNone);
  448.     swa.border_pixel = 0;
  449.     swa.colormap = cmap;
  450.     swa.event_mask = ExposureMask | StructureNotifyMask | KeyPressMask
  451.     | KeyReleaseMask;
  452.     window = XCreateWindow(dpy, RootWindow(dpy, vi->screen), 10, 10,
  453.                width, height,
  454.                0, vi->depth, InputOutput, vi->visual,
  455.                CWBorderPixel|CWColormap|CWEventMask, &swa);
  456.     XSetStandardProperties(dpy, window, "ttri", "ttri", None, argv, argc, NULL);
  457.     XSetWMColormapWindows(dpy, window, &window, 1);
  458.     XMapWindow(dpy, window);
  459.     XIfEvent(dpy, &event, WaitForMapNotify, (char*)window);
  460.  
  461.     cx = glXCreateContext(dpy, vi, 0, GL_TRUE);
  462.     if (!glXMakeCurrent(dpy, window, cx)) {
  463.     fprintf(stderr, "Can't make window current to context\n");
  464.     return -1;
  465.     }
  466.  
  467.     glLineStipple(1, 0xF0F0);
  468.     needDisplay = GL_TRUE;
  469.     for (;;) {
  470.     do {
  471.         XNextEvent(dpy, &event);
  472.         switch (event.type) {
  473.           case Expose:
  474.         needDisplay = GL_TRUE;
  475.         break;
  476.           case ConfigureNotify:
  477.         width = event.xconfigure.width;
  478.         height = event.xconfigure.height;
  479.         needDisplay = GL_TRUE;
  480.         break;
  481.           case KeyPress:
  482.         {
  483.             char buf[100];
  484.             int rv;
  485.             KeySym ks;
  486.  
  487.             rv = XLookupString(&event.xkey, buf, sizeof(buf), &ks, 0);
  488.             switch (ks) {
  489.               case XK_Left:
  490.             zRotation += 0.5;
  491.             needDisplay = GL_TRUE;
  492.             break;
  493.               case XK_Right:
  494.             zRotation -= 0.5;
  495.             needDisplay = GL_TRUE;
  496.             break;
  497.               case XK_Up:
  498.             zoom *= 0.75;
  499.             needDisplay = GL_TRUE;
  500.             break;
  501.               case XK_Down:
  502.             zoom /= 0.75;
  503.             if (zoom > 10) {
  504.                 zoom = 10;
  505.             }
  506.             needDisplay = GL_TRUE;
  507.             break;
  508.               case XK_f: case XK_F:
  509.             glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  510.             needDisplay = GL_TRUE;
  511.             break;
  512.               case XK_p: case XK_P:
  513.             glPolygonMode(GL_FRONT_AND_BACK, GL_POINT);
  514.             needDisplay = GL_TRUE;
  515.             break;
  516.               case XK_l: case XK_L:
  517.             glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  518.             needDisplay = GL_TRUE;
  519.             break;
  520.               case XK_1:
  521.             state = SOLID;
  522.             needDisplay = GL_TRUE;
  523.             break;
  524.               case XK_2:
  525.             state = LINE;
  526.             needDisplay = GL_TRUE;
  527.             break;
  528.               case XK_3:
  529.             state = POINT;
  530.             needDisplay = GL_TRUE;
  531.             break;
  532.               case XK_A: case XK_a:
  533.             zRotation = 0.0;
  534.             needDisplay = GL_TRUE;
  535.             break;
  536.               case XK_B: case XK_b:
  537.             zRotation = 90.0;
  538.             needDisplay = GL_TRUE;
  539.             break;
  540.               case XK_C: case XK_c:
  541.             zRotation = 180.0;
  542.             needDisplay = GL_TRUE;
  543.             break;
  544.               case XK_D: case XK_d:
  545.             zRotation = 270.0;
  546.             needDisplay = GL_TRUE;
  547.             break;
  548.               case XK_V: case XK_v:
  549.             showVerticies = !showVerticies;
  550.             needDisplay = GL_TRUE;
  551.             break;
  552.               case XK_s: case XK_S:
  553.             glShadeModel(GL_SMOOTH);
  554.             needDisplay = GL_TRUE;
  555.             break;
  556.               case XK_t: case XK_T:
  557.             glShadeModel(GL_FLAT);
  558.             needDisplay = GL_TRUE;
  559.             break;
  560.               case XK_h: case XK_H:
  561.             hideBottomTriangle = !hideBottomTriangle;
  562.             needDisplay = GL_TRUE;
  563.             break;
  564.               case XK_o: case XK_O:
  565.             outline = !outline;
  566.             needDisplay = GL_TRUE;
  567.             break;
  568.               case XK_m: case XK_M:
  569.             dithering = !dithering;
  570.             needDisplay = GL_TRUE;
  571.             break;
  572.               case XK_7:
  573.             culling = !culling;
  574.             needDisplay = GL_TRUE;
  575.             needUpdate=1;
  576.             break;
  577.               case XK_8:
  578.             winding = !winding;
  579.             needDisplay = GL_TRUE;
  580.             needUpdate=1;
  581.             break;
  582.               case XK_9:
  583.             face = !face;
  584.             needDisplay = GL_TRUE;
  585.             needUpdate=1;
  586.             break;
  587.               case XK_Q: case XK_q:
  588.             glEnable(GL_POLYGON_SMOOTH);
  589.             glEnable(GL_BLEND);
  590.             glBlendFunc(GL_SRC_ALPHA, GL_ONE);
  591.             if (!rgb) {
  592.                 color1 = 32;
  593.                 color2 = 128;
  594.                 color3 = 224;
  595.             }
  596.             needDisplay = GL_TRUE;
  597.             break;
  598.               case XK_W: case XK_w:
  599.             glDisable(GL_POLYGON_SMOOTH);
  600.             glDisable(GL_BLEND);
  601.             if (!rgb) {
  602.                 color1 = 18;
  603.                 color2 = 24;
  604.                 color3 = 30;
  605.             }
  606.             needDisplay = GL_TRUE;
  607.             break;
  608.               case XK_Escape:
  609.             return 0;
  610.             }
  611.         }
  612.         break;
  613.         }
  614.     } while (XPending(dpy) != 0);
  615.  
  616.     if (needDisplay) {
  617.         needDisplay = GL_FALSE;
  618.         DoDisplay();
  619.     }
  620.     }
  621. }
  622.